/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//Xiong@2011.11.22: merge

package com.android.launcherplus;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ActionButton extends ImageView  {
	private Launcher mLauncher;

    private final boolean DEBUG = false;
    private final String LOG_TAG = "ActionButton";

    private IconCache mIconCache;
	private ItemInfo mCurrentInfo;
	private Drawable mIconNormal;
    
	public ActionButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public ActionButton(Context context, AttributeSet attrs) {
		this(context, attrs,0);
		// TODO Auto-generated constructor stub
	}

	public ActionButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		setHapticFeedbackEnabled(true);
		LauncherApplication app = (LauncherApplication) context.getApplicationContext();
        mIconCache = app.getIconCache();
	}

    void setLauncher(Launcher launcher) {
        mLauncher = launcher;
    }

	protected void updateLaunchInfo(ItemInfo info){
    	mCurrentInfo = info;
		//TODO: extract icon and put it as the imageview src...
		Drawable myIcon = null;
		if (info != null && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
	        myIcon = new FastBitmapDrawable(((ShortcutInfo)info).getIcon(mIconCache));
		}
        setIcon(myIcon);
        invalidate();
	}

	public void updateIcon(){
    	if(mCurrentInfo != null){
			ItemInfo info = mCurrentInfo;
			Drawable myIcon = null;
	        if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
				myIcon = new FastBitmapDrawable(((ShortcutInfo)info).getIcon(mIconCache));
	        }
	        setIcon(myIcon);
	        invalidate();
    	}
	}

	private void setIcon(Drawable d){
	    if(mIconNormal != null){
	        mIconNormal.setCallback(null);
	        mIconNormal = null;
	    }
	    mIconNormal = d;
		setImageDrawable(mIconNormal);
	}
}
